﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace ARS.PublicTool.DataHandle
{
    /// <summary>
    /// 序列化json类
    /// </summary>
    public class JsonSerializer
    {
        private static JavaScriptSerializer ser = new JavaScriptSerializer();
        /// <summary>
        /// 序列化JAVA对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeJava(object obj)
        {
            return ser.Serialize(obj);
        }

        /// <summary>
        /// 序列化JAVA对象
        /// </summary>
        /// <param name="code"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static string GetJson(int Code, string Remark)
        {
            return "{\"Code\":" + Code + ",\"Remark\":\"" + Remark + "\"}";
        }
        /// <summary>
        /// 序列化JAVA对象
        /// </summary>
        /// <param name="code"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static string GetJsonJava(int Code, string Remark)
        {
            return "{\"Code\":" + Code + ",\"Remark\":\"" + Remark + "\"}";
        }
        #region DataTable转换json

        /// <summary>
        ///  DataTable转换为json字符串,如{"a":"b"}
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DtToJson(DataTable dt,string[] columnList=null)
        {
            if (dt==null)
            {
                return "";
            }
            int num = dt.Rows.Count;
            if (num > 0)
            {
                int num_c = dt.Columns.Count;
                StringBuilder sb = new StringBuilder();
                //sb.Append("[");
                for (int i = 0; i < num; i++)
                {
                    sb.Append("{");
                    for (int j = 0; j < num_c; j++)
                    {
                        string columnName = dt.Columns[j].ColumnName;
                        if (columnList != null)
                        {
                            if (Array.IndexOf<string>(columnList, columnName) == -1)
                            {
                                continue;
                            }
                        }
                        sb.Append(string.Format("\"{0}\":\"{1}\",", columnName, dt.Rows[i][j].ToString()));
                    }
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append("},");
                }
                sb.Remove(sb.Length - 1, 1);
                //sb.Append("]");
                return sb.ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        ///  DataTable转换为json列表如:[{"a":"b"}]
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DtToJsonList(DataTable dt, string[] columnList = null)
        {
            string json = DtToJson(dt, columnList);
            if (json=="")
            {
                return "[]";
            }
            else
            {
                return "[" + json + "]";               
            }
        }
        /// <summary>
        ///  DataTable转换为json列表:[]
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="listName"></param>
        /// <returns></returns>
        public static string DtToJsonList(DataTable dt, string listName)
        {
            return string.Format("\"{0}\":{1}", listName, DtToJsonList(dt));
        }
        /// <summary>
        ///  DataTable转换为json列表:[]
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="listName"></param>
        /// <param name="countName"></param>
        /// <returns></returns>
        public static string DtToJsonList(DataTable dt,string listName,string countName)
        {
            return string.Format("\"{0}\":{1},\"{2}\":{3}", countName, dt.Rows.Count, listName, DtToJsonList(dt));
        }
        #endregion

        /// <summary>
        /// 从流中获取json字符串
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string InputToJsonStr(HttpContext context)
        {
            byte[] byts = new byte[context.Request.InputStream.Length];
            context.Request.InputStream.Read(byts, 0, byts.Length);
            string req = Encoding.UTF8.GetString(byts);
            req = context.Server.UrlDecode(req);
            return req;
        }
    }
}
